package com.quanxiaoha.weblog.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quanxiaoha.weblog.common.domain.entity.ScheduleSkillStage;
import com.quanxiaoha.weblog.common.domain.mapper.ScheduleSkillStageMapper;
import com.quanxiaoha.weblog.common.domain.vo.NodeVo;
import com.quanxiaoha.weblog.common.domain.vo.ScheduleSkillVo;
import com.quanxiaoha.weblog.common.domain.vo.StageVo;
import com.quanxiaoha.weblog.common.service.IScheduleSkillStageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Roy
 * @since 2025-06-08
 */
@Service
@Slf4j
public class ScheduleSkillStageServiceImpl extends ServiceImpl<ScheduleSkillStageMapper, ScheduleSkillStage> implements IScheduleSkillStageService {

    @Resource
    ThreadPoolTaskExecutor taskExecutor;


    @Override
    public List<StageVo> getStageList(Integer skillId) {

        LambdaQueryWrapper<ScheduleSkillStage> qw = new LambdaQueryWrapper<>();
        qw.eq(ScheduleSkillStage::getSkillId, skillId);
        List<ScheduleSkillStage> list = this.list(qw);
        if (list.isEmpty()) {return new ArrayList<>();}
        List<StageVo> stageVoList = transform(list);
        return stageVoList;
    }

    @Override
    @Async("taskExecutor")
    public void saveStage(Integer skillId , List<StageVo> stageVos) {

        try {
            List<ScheduleSkillStage> scheduleSkillStages = new ArrayList<>();
            LocalDateTime now = LocalDateTime.now();
            stageVos.forEach(stageVo -> {
                stageVo.getNodes().forEach(nodeVo -> {
                    ScheduleSkillStage scheduleSkillStage = new ScheduleSkillStage();
                    scheduleSkillStage.setSkillId(skillId);
                    scheduleSkillStage.setStageName(stageVo.getName());
                    scheduleSkillStage.setNodeName(nodeVo.getName());
                    scheduleSkillStage.setCompleted(1);
                    scheduleSkillStage.setCreateTime(now);
                    scheduleSkillStages.add(scheduleSkillStage);
                });
            });
            this.saveBatch(scheduleSkillStages);
            log.info("Stages存储成功");
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

    public static List<StageVo> transform(List<ScheduleSkillStage> originalList) {

        // 1. 按阶段分组
        Map<String, List<ScheduleSkillStage>> map = originalList.stream()
                .collect(Collectors.groupingBy(ScheduleSkillStage::getStageName));

        // 2. 转换为目标结构
        List<StageVo> result = new ArrayList<>();
        map.forEach((stageName, items) -> {
            StageVo stageVo = new StageVo();
            stageVo.setName(stageName);

            // 3. 处理每个阶段的节点
            List<NodeVo> nodes = items.stream()
                    .map(item -> {
                        NodeVo nodeVo = new NodeVo();
                        nodeVo.setId(item.getId());
                        nodeVo.setName(item.getNodeName());
                        nodeVo.setCompleted(item.getCompleted()==2?true:false);
                        return nodeVo;
                    }).collect(Collectors.toList());

            stageVo.setNodes(nodes);
            result.add(stageVo);
        });

        return result;
    }

}
